package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterConfig;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
 * @author: atguigu
 * @create: 2023-09-16 15:41
 */
@Slf4j
@Component
public class DeadLetterMsgReceiver {


    /**
     * @param data
     * @param channel 信道
     * @param message 消息对象
     */
    @SneakyThrows
    @RabbitListener(queues = DeadLetterConfig.queue_dead_2)
    public void receiverMessage(String data, Channel channel, Message message) {
        if (StringUtils.isNotBlank(data)) {
            log.info("[消费者]监听到基于死信实现的延迟消息：{}", data);
        }
        //默认消费者:Broker服务器只负责将消息投递，无论消费者业务成功或者失败，Broker自动将消息删除
        //消费者端进行手动应答：Broker服务器投递消息后，一致等待消费者进行应答，Broker收到应答才会将MQ队列中消息删除
        //basicAck业务正常执行完毕 p1：消息唯一标识从1开始递增 p2:是否批量确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        //basicNack业务非正常执行-进行重试（设置上限） p1:消息标识 p2:是否批量确认消息 p3:是否重新入队
        //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

        //basicReject消费者明确拒绝接受消息，被拒绝消息会进入死信队列 p1:消息标识 p3:是否重新入队
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
    }

}
